<?php
defined('SYSPATH') or die('No direct access allowed.');

class Model_Member_Order_Verify extends ORM
{


    /**
     * @function 验单
     * @param $orderid
     * @param $dingnum
     * @param null $order_info
     * @return bool
     */
    public static function check_ticket($orderid,$dingnum=0,$childnum=0,$oldnum=0,$account_id,$order_info = null)
    {

        if(empty($order_info))
        {
            $order_info = DB::select()->from('member_order')->where('id','=',$orderid)->execute()->current();
        }
        if(empty($order_info))
        {
            return false;
        }

        try {

            $verified_num_info = self::get_verified_num_info($orderid);
            $available_dingnum = $order_info['dingnum']-$verified_num_info['dingnum'];
            $available_childnum = $order_info['childnum']-$verified_num_info['childnum'];
            $available_oldnum = $order_info['oldnum']-$verified_num_info['oldnum'];

            if($dingnum>0 && intval($dingnum)>intval($available_dingnum))
            {
                throw new Exception('验单数量超过可验数量');
            }
            if($childnum>0 && intval($childnum)>intval($available_childnum))
            {
                throw new Exception('验单数量超过可验数量');
            }
            if($oldnum>0 && intval($oldnum)>intval($available_oldnum))
            {
                throw new Exception('验单数量超过可验数量');
            }

            $verify_model = ORM::factory('member_order_verify');
            $verify_model->orderid = $orderid;
            $verify_model->dingnum = $dingnum;
            $verify_model->childnum = $childnum;
            $verify_model->oldnum = $oldnum;
            $verify_model->addtime = time();
            $verify_model->save();
            if (!$verify_model->saved())
            {
                throw new Exception('系统错误');
            }

            $new_status = $order_info['status'];
            //如果是全部验完
            if($dingnum==$available_dingnum && $childnum == $available_childnum && $oldnum==$available_oldnum)
            {
                $update_arr= array(
                    'isconsume' => 1,
                    'status' => 5,
                    'consumetime' => time(),
                    'consumeverifyuser' => $account_id,
                    'finishtime' => time(),
                    'consumeverifymemo' => '兑票'
                );
                $order_flag = DB::update('member_order')->set($update_arr)->where('id','=',$orderid)->and_where('status','in',array('2','5'))->and_where('isconsume','=','0')->execute();
                if(!$order_flag)
                {
                    throw new Exception('系统错误');
                }
                $new_status = 5;

            }

            $update_eticket= array(
                'status' => 1,
                'consume_time' => time(),
            );
            DB::update('member_order_eticket')->set($update_eticket)
                ->where('order_id','=',$order_info['id'])
                ->and_where('status','=',0)->limit($dingnum+$childnum+$oldnum)->execute();


            $verify_model->reload();
            $verify_info = $verify_model->as_array();
            return array('status'=>1,'order_status'=>$new_status,'verify_info'=>$verify_info);
        }catch (Exception $e)
        {
            return array('status'=>0,'msg'=>$e->getMessage());
        }

    }

    /**
     * @function 获取已验单数量
     * @param $orderid
     */
    public static function get_verified_num_info($orderid)
    {
        $verify_list= DB::query(Database::SELECT,"select *  from sline_member_order_verify where orderid='{$orderid}' ")->execute()->as_array();
        $num_info = array('dingnum'=>0,'childnum'=>0,'oldnum'=>0,'total'=>0);
        foreach($verify_list as $verify)
        {
           $num_info['dingnum'] += $verify['dingnum'];
           $num_info['childnum'] += $verify['childnum'];
           $num_info['oldnum'] += $verify['oldnum'];
           $num_info['total'] =  $verify['dingnum']+$verify['childnum']+ $verify['oldnum'];
        }
        return $num_info;
    }


    /**
     * @function 获取验单列表
     * @param $orderid
     */
    public static function get_verified_list($orderid)
    {
        $list = DB::select()->from('member_order_verify')->where('orderid','=',$orderid)->order_by('addtime','asc')->execute()->as_array();
        return $list;
    }
}